package com.phantasy.scene
{
	import com.phantasy.globals.GameParameters;
	import com.phantasy.loaders.CommonLocator;
	import com.phantasy.scene.sceneData.CityVo;
	import com.phantasy.scene.sceneData.MacroPathVo;
	import com.phantasy.scene.sceneData.MapDataVo;
	import com.phantasy.scene.sceneData.MapEncode;
	import com.phantasy.scene.tile.Pt;
	import com.phantasy.scene.tile.gameAstar.MCAstar;
	import com.phantasy.scene.tile.gameAstar.WorldMapGrid;
	import com.zip.ZipEntry;
	import com.zip.ZipFile;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	/**
	 *负责解析地图 
	 * @author Liseen
	 * 
	 */	
	public class WorldManager
	{
		private static var allMapsDict:Dictionary;
		private static var worldMapData:MapDataVo;
		private static var worldGrid:WorldMapGrid;
		private static var astar:MCAstar;
		private static var mapsXML:XML;
		private static var citys:Dictionary;
		public function WorldManager()
		{
			
		}
		
		public static function setup():void
		{
			parseXML();
		}
		/**
		 *解析mcm 
		 * @param data
		 * 
		 */		
		public static function parseMcmBag(data:ByteArray):void
		{
			var zipEntry:ZipEntry;
			var uncompressData:ByteArray;
			var fileName:String;
			if(allMapsDict==null){
				allMapsDict = new Dictionary();
			}
			
			var zipFile:ZipFile = new ZipFile(data);
			var index:int=0;
			while(index<zipFile.entries.length){
				zipEntry = zipFile.entries[index];
				uncompressData = zipFile.getInput(zipEntry);
				if (zipEntry.name.indexOf(".mcm") != -1)
				{
					fileName = zipEntry.name.substr(0, 5);
					if (allMapsDict[fileName] == null)
					{
						allMapsDict[fileName] = uncompressData;
						if (fileName == "world")
						{
							parseMCM();
						}
					}
				}
				index++;
			}
		}
		
		public static function getMapDataVo(_arg1:int):MapDataVo
		{
			var _local2:Object = allMapsDict[_arg1.toString()];
			if (_local2)
			{
				if ((_local2 is ByteArray))
				{
					allMapsDict[_arg1] = MapEncode.encodeByteArray((_local2 as ByteArray));
					return (allMapsDict[_arg1]);
				}
				if ((_local2 is MapDataVo))
				{
					return ((_local2 as MapDataVo));
				}
			}
			return (null);
		}
		
		public static function getCityVo(_arg1:int):CityVo
		{
			if (citys[(_arg1 + "")] == null)
			{
				throw (new Error((("找不到地图ID为：" + _arg1) + "的配置")));
			}
			return ((citys[(_arg1 + "")] as CityVo));
		}
		
		public static function getMapName(_arg1:int):String
		{
			var _local2 = "";
			var _local3:CityVo = getCityVo(_arg1);
			if (_local3 != null)
			{
				_local2 = _local3.name;
			}
			return (_local2);
		}
		
		private static function parseMCM():void
		{
			var data:ByteArray = allMapsDict["world"];
			worldMapData = MapEncode.encodeByteArray(data);
			worldGrid = new WorldMapGrid(worldMapData);
			astar = new MCAstar(worldGrid);
		}
		
		private static function parseXML():void
		{
			var _local2:XMLList;
			var _local3:int;
			var _local4:MacroPathVo;
			var _local5:XML;
			var _local6:XML;
			var _local7:int;
			var _local8:XMLList;
			var _local9:int;
			var _local10:int;
			var _local11:CityVo;
			var _local12:XMLList;
			var _local13:int;
			var _local14:int;
			var _local15:XMLList;
			var _local16:int;
			var _local17:int;
			var _local18:XMLList;
			var _local19:int;
			var _local20:int;
			var _local21:XMLList;
			var _local22:int;
			var _local23:int;
			citys = new Dictionary();
			var _local1:XML = CommonLocator.getXML(CommonLocator.WORLD_URL);
			if (_local1)
			{
				mapsXML = _local1;
				_local2 = mapsXML.child("country");
				_local3 = _local2.length();
				_local7 = 0;
				while (_local7 < _local3)
				{
					_local8 = _local2[_local7].child("map");
					_local9 = _local8.length();
					_local10 = 0;
					while (_local10 < _local9)
					{
						_local6 = _local8[_local10];
						_local11 = new CityVo();
						_local11.init();
						_local11.id = int(_local6.@id);
						_local11.name = _local6.@name;
						_local11.url = _local6.@url;
						_local11.posx = int(String(_local6.@pos).split("|")[0]);
						_local11.posy = int(String(_local6.@pos).split("|")[1]);
						_local11.scale = Number(_local6.@scale);
						_local11.music = ((GameParameters.getInstance().resourceHost + "/") + String(_local6.@music));
						_local11.turn_map_abled = int(_local6.@turn_map_abled);
						_local11.level = int(_local6.@index);
						_local11.countryId = int(_local11.id.toString().substr(0, 2));
						_local12 = _local6.child("children")[0].child("point");
						_local13 = _local12.length();
						_local14 = 0;
						while (_local14 < _local13)
						{
							_local5 = _local12[_local14];
							_local4 = new MacroPathVo(int(_local5.@tar_map), new Pt(int(_local5.@x), 0, int(_local5.@y)));
							if (_local11.children[_local4.mapid.toString()] == null)
							{
								_local11.children[_local4.mapid.toString()] = _local4;
							};
							_local14++;
						};
						_local15 = _local6.child("prents")[0].child("point");
						_local16 = _local15.length();
						_local17 = 0;
						while (_local17 < _local16)
						{
							_local5 = _local15[_local17];
							_local4 = new MacroPathVo(int(_local5.@tar_map), new Pt(int(_local5.@x), 0, int(_local5.@y)));
							if (_local11.parents[_local4.mapid.toString()] == null)
							{
								_local11.parents[_local4.mapid.toString()] = _local4;
							};
							_local17++;
						};
						_local18 = _local6.child("renascence")[0].child("map");
						_local19 = _local18.length();
						_local20 = 0;
						while (_local20 < _local19)
						{
							_local5 = _local18[_local20];
							_local4 = new MacroPathVo(int(_local5.@tar_map), null);
							_local11.renascence.push(_local4);
							_local20++;
						};
						_local21 = _local6.child("livePoints")[0].child("point");
						_local22 = _local21.length();
						_local23 = 0;
						while (_local23 < _local22)
						{
							_local5 = _local21[_local23];
							_local4 = new MacroPathVo(int(_local5.@tar_map), new Pt(int(_local5.@x), 0, int(_local5.@y)));
							_local11.livePoints.push(_local4);
							_local23++;
						}
						if (citys[_local11.id] == null)
						{
							citys[_local11.id] = _local11;
						}
						_local10++;
					}
					_local7++;
				}
			}
		}
	}
}